package net.jdm.constraint;

import java.util.HashSet;
import java.util.Set;

import net.jdm.constraint.cycle.CycleConstraint;
import net.jdm.constraint.dependence.DependenceConstraint;
import net.jdm.constraint.distance.DistanceConstraint;
import net.jdm.constraint.instability.InstabilityDependConstraint;
import net.jdm.domain.entity.Component;
import net.jdm.domain.entity.Package;


public class PackagesConstraint{
	private DistanceConstraint distanceConstraint;
	private InstabilityDependConstraint packageInstabilityConstraint;
	private Set<DependenceConstraint> packageDependencyConstraints= new HashSet<DependenceConstraint>();
	private CycleConstraint cycleConstraint;
	public void setPackageDependencyConstraints(
			Set<DependenceConstraint> packageDependencyConstraints) {
		this.packageDependencyConstraints = packageDependencyConstraints;
	}

	public Set<DependenceConstraint> getPackageDependencyConstraints() {
		return packageDependencyConstraints;
	}

	public void setDistanceConstraint(DistanceConstraint distanceConstraint) {
		this.distanceConstraint = distanceConstraint;
	}

	public DistanceConstraint getDistanceConstraint() {
		return distanceConstraint;
	}
    public boolean checkConstraint(Component component){
    	boolean pass=true;
    	if(this.packageDependencyConstraints!=null)
    	for(DependenceConstraint dependenceConstraintType:this.packageDependencyConstraints){
    		pass=dependenceConstraintType.check(component)&&pass;
    	}
    	if(this.distanceConstraint!=null)
    	  pass=checkAllPackagesDistance(component)&&pass;
    	if(this.cycleConstraint!=null)
    	  pass=this.cycleConstraint.check(component)&&pass;
		if(this.packageInstabilityConstraint!=null){
			pass=packageInstabilityConstraint.check(component)&&pass;
		}
    	return pass;
    }
    public boolean checkConstraint(Package actualJavaPackage){
    	if(distanceConstraint!=null)
    	   return distanceConstraint.check(actualJavaPackage);
    	return true;
    }
	public void setPackageCycleConstraint(CycleConstraint cycleConstraint) {
		  this.cycleConstraint=cycleConstraint;
	}
	public void setPackageInstabilityConstraint(
			InstabilityDependConstraint packageInstabilityConstraint) {
		this.packageInstabilityConstraint = packageInstabilityConstraint;
	}
	public InstabilityDependConstraint getPackageInstabilityConstraint() {
		return this.packageInstabilityConstraint;
	}

	public boolean checkAllPackagesDistance(Component actualComponent) {
		boolean pass=true;
		for(Package javaPackage:actualComponent.getPackages()){
			   pass=distanceConstraint.check(javaPackage)&&pass;
		}
		return pass;
	}
}
